#!/bin/bash
[% c("var/set_default_env") -%]
builddir=/var/tmp/build
distdir=/var/tmp/dist/mingw-w64-clang
mkdir -p $distdir $builddir

cd /var/tmp/dist
tar -xf $rootdir/[% c('input_files_by_name/clang') %]
mv clang/* mingw-w64-clang/
tar -xf $rootdir/[% c('input_files_by_name/cmake') %]

cd $builddir
tar -xf $rootdir/[% c('input_files_by_name/llvm-project') %]

# libc++.a depends on libunwind.a. While the linker will automatically link to
# libc++.a in C++ mode, it won't pick libunwind.a, requiring the caller to
# explicitly pass -lunwind. To work around that, we merge libunwind.a into
# libc++.a.
merge_libs() {
  cat <<EOF |llvm-ar -M
CREATE tmp.a
ADDLIB $1
ADDLIB $2
SAVE
END
EOF
  llvm-ranlib tmp.a
  mv tmp.a $1
}

build_mingw() {
  arch=$1
  compiler_rt_machine=$2
  crt_flags=$3

  # Adding the wrappers and symlinks we need
  cd $distdir/bin
  compiler_flags="--sysroot \$DIR/../$arch-w64-mingw32 -rtlib=compiler-rt -stdlib=libc++ -fuse-ld=lld -fuse-cxa-atexit -Qunused-arguments"

  cat <<EOF >"$arch-w64-mingw32-clang"
#!/bin/sh
DIR="\$(cd "\$(dirname "\$0")" && pwd)"
\$DIR/clang -target $arch-w64-mingw32 $compiler_flags "\$@"
EOF
  chmod +x "$arch-w64-mingw32-clang"

  cat <<EOF >"$arch-w64-mingw32-clang++"
#!/bin/sh
DIR="\$(cd "\$(dirname "\$0")" && pwd)"
\$DIR/clang -target $arch-w64-mingw32 --driver-mode=g++ $compiler_flags "\$@"
EOF
  chmod +x "$arch-w64-mingw32-clang++"

  ln -s "$arch-w64-mingw32-clang" "$arch-w64-mingw32-cc"
  ln -s "$arch-w64-mingw32-clang++" "$arch-w64-mingw32-cxx"
  ln -s llvm-ar "$arch-w64-mingw32-ar"
  ln -s llvm-nm "$arch-w64-mingw32-nm"
  ln -s llvm-strip "$arch-w64-mingw32-strip"
  ln -s llvm-ranlib "$arch-w64-mingw32-ranlib"
  ln -s llvm-readobj "$arch-w64-mingw32-readobj"
  ln -s llvm-objcopy "$arch-w64-mingw32-objcopy"
  ln -s llvm-windres "$arch-w64-mingw32-windres"

  # Building mingw-w64
  export PATH="$distdir/bin:/var/tmp/dist/cmake/bin:$PATH"
  CC="$arch-w64-mingw32-clang"
  CXX="$arch-w64-mingw32-clang++"
  # This is the default value of _WIN32_WINNT. Gecko configure script explicitly
  # sets this, so this is not used to build Gecko itself. We default to 0x601,
  # which is Windows 7.
  default_win32_winnt=0x601

  # Restart from a clean state.
  cd $builddir
  rm -rf mingw-w64-clang
  tar -xf $rootdir/mingw-w64-clang-[% c('version') %].tar.[% c('compress_tar') %]
  mv mingw-w64-clang-[% c('version') %] mingw-w64-clang

  # Apply the same patches as Firefox
  cd $builddir/mingw-w64-clang
  patch -p1 < "$rootdir/mingw-dwrite_3.patch"
  patch -p1 < "$rootdir/mingw-unknown.patch"
  patch -p1 < "$rootdir/mingw-enum.patch"
  patch -p1 < "$rootdir/mingw-widl.patch"
  patch -p1 < "$rootdir/mingw-dispatchqueue.patch"
  patch -p1 < "$rootdir/mingw-ts_sd.patch"
  patch -p1 < "$rootdir/mingw-composition.patch"

  cd $builddir/mingw-w64-clang/mingw-w64-headers
  mkdir build && cd build
  ../configure --host="$arch-w64-mingw32" \
               --enable-sdk=all \
               --enable-idl \
               --with-default-msvcrt=ucrt \
               --with-default-win32-winnt=$default_win32_winnt \
               --prefix=$distdir/$arch-w64-mingw32
  make -j[% c("num_procs") %] install

  cd $builddir/mingw-w64-clang/mingw-w64-crt
  mkdir "build-$arch" && cd "build-$arch"
  ../configure --host="$arch-w64-mingw32" \
               $crt_flags \
               --with-default-msvcrt=ucrt \
               CC="$CC" \
               AR=llvm-ar \
               RANLIB=llvm-ranlib \
               DLLTOOL=llvm-dlltool \
               --prefix=$distdir/$arch-w64-mingw32
  make -j[% c("num_procs") %]
  make -j[% c("num_procs") %] install

  cd $builddir/mingw-w64-clang/mingw-w64-tools/widl
  mkdir "build-$arch" && cd "build-$arch"
  ../configure --target="$arch-w64-mingw32" --prefix=$distdir
  make -j[% c("num_procs") %]
  make -j[% c("num_procs") %] install

  # compiler-rt
  cd $builddir/clang-source/compiler-rt
  mkdir "build-$arch" && cd "build-$arch"
  cmake -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_C_COMPILER=$CC \
        -DCMAKE_SYSTEM_NAME=Windows \
        -DCMAKE_AR=$distdir/bin/llvm-ar \
        -DCMAKE_RANLIB=$distdir/bin/llvm-ranlib \
        -DCMAKE_C_COMPILER_WORKS=1 \
        -DCMAKE_C_COMPILER_TARGET=$compiler_rt_machine-windows-gnu \
        -DCOMPILER_RT_DEFAULT_TARGET_ONLY=TRUE \
        $builddir/clang-source/compiler-rt/lib/builtins
  make -j[% c("num_procs") %]
  rtdir=$distdir/lib/clang/[% pc("llvm-project", "var/llvm_major") %]/lib/windows
  mkdir -p $rtdir
  cp lib/windows/libclang_rt.builtins-$compiler_rt_machine.a $rtdir/

  # libc++, libc++abi and libunwind now are compiled together
  # See https://libcxx.llvm.org/BuildingLibcxx.html
  cd $builddir/clang-source
  mkdir "build-$arch"
  cmake -G "Unix Makefiles" -S runtimes -B "build-$arch" \
        -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX=$distdir/$arch-w64-mingw32 \
        -DCMAKE_C_COMPILER=$CC \
        -DCMAKE_CXX_COMPILER=$CXX \
        -DCMAKE_CROSSCOMPILING=TRUE \
        -DCMAKE_SYSROOT=$distdir/$arch-w64-mingw32 \
        -DCMAKE_SYSTEM_NAME=Windows \
        -DCMAKE_C_COMPILER_WORKS=TRUE \
        -DCMAKE_CXX_COMPILER_WORKS=TRUE \
        -DLLVM_COMPILER_CHECKED=True \
        -DCMAKE_AR=$distdir/bin/llvm-ar \
        -DCMAKE_RANLIB=$distdir/bin/llvm-ranlib \
        -DLLVM_NO_OLD_LIBSTDCXX=TRUE \
        -DCXX_SUPPORTS_CXX11=TRUE \
        -DCXX_SUPPORTS_CXX_STD=True \
        -DCMAKE_CXX_FLAGS="-Wno-dll-attribute-on-redeclaration -DPSAPI_VERSION=2 -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCPP_HAS_THREAD_API_WIN32 -D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS" \
        -DCMAKE_C_FLAGS="-Wno-dll-attribute-on-redeclaration" \
        -DLIBUNWIND_USE_COMPILER_RT=TRUE \
        -DLIBUNWIND_ENABLE_THREADS=TRUE \
        -DLIBUNWIND_ENABLE_SHARED=FALSE \
        -DLIBUNWIND_ENABLE_CROSS_UNWINDING=FALSE \
        -DLIBCXXABI_USE_COMPILER_RT=ON \
        -DLIBCXXABI_ENABLE_EXCEPTIONS=ON \
        -DLIBCXXABI_ENABLE_THREADS=ON \
        -DLIBCXXABI_TARGET_TRIPLE=$arch-w64-mingw32 \
        -DLIBCXXABI_ENABLE_SHARED=OFF \
        -DLIBCXXABI_LIBCXX_INCLUDES=$builddir/clang-source/libcxx/include \
        -DLIBCXX_USE_COMPILER_RT=ON \
        -DLIBCXX_INSTALL_HEADERS=ON \
        -DLIBCXX_ENABLE_EXCEPTIONS=ON \
        -DLIBCXX_ENABLE_THREADS=ON \
        -DLIBCXX_HAS_WIN32_THREAD_API=ON \
        -DLIBCXX_ENABLE_MONOTONIC_CLOCK=ON \
        -DLIBCXX_ENABLE_SHARED=OFF \
        -DLIBCXX_SUPPORTS_STD_EQ_CXX11_FLAG=TRUE \
        -DLIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB=TRUE \
        -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF \
        -DLIBCXX_ENABLE_FILESYSTEM=OFF \
        -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=TRUE \
        -DLIBCXX_CXX_ABI=libcxxabi \
        -DLIBCXX_CXX_ABI_INCLUDE_PATHS=$builddir/clang-source/libcxxabi/include \
        -DLIBCXX_CXX_ABI_LIBRARY_PATH=$builddir/clang-source/libcxxabi/build/lib \

  cd "build-$arch"
  make -j[% c("num_procs") %] VERBOSE=1
  make install

  merge_libs $distdir/$arch-w64-mingw32/lib/libc++.a $distdir/$arch-w64-mingw32/lib/libunwind.a

  # Do not add libssp anymore. Use the same trick as llvm-mingw.
  # See https://github.com/mstorsjo/llvm-mingw/commit/fb67e16120b05c0664503b17532d5cc28c9cd1e9
  llvm-ar rcs "$distdir/$arch-w64-mingw32/lib/libssp.a"
  llvm-ar rcs "$distdir/$arch-w64-mingw32/lib/libssp_nonshared.a"
}

build_mingw x86_64 "x86_64" "--disable-lib32 --enable-lib64"
build_mingw i686 "i386" "--enable-lib32 --disable-lib64"

# Packaging up everything
cd $distdir
cd ..
[% c('tar', {
        tar_src => [ project ],
        tar_args => '-caf ' _ dest_dir _ '/' _ c('filename'),
    }) %]
